/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.ical;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import net.sourceforge.jtimepiece.TimeUtil;

/**
 *
 * @author Administrator
 */
public class ERPCalendar {
        String name;
        IEventStore repository;
        Date start,end;
        Color fg, bg, border;
        private boolean selected;
        protected HashMap<String, Vector> cachedERPEvents;
        private static SimpleDateFormat FORMAT_YMD=new SimpleDateFormat("yyyy/MM/dd");

        public ERPCalendar(IEventStore repository,String name) {
            this.repository = repository;
            this.name=name;
            this.bg = Color.blue;
            this.fg = Color.white;
	    this.border = Color.white;
            cachedERPEvents=new HashMap<String, Vector>();
            selected=true;
        }

       public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Date getStart() {
            return start;
        }

        public void setStart(Date Start) {
            this.start = Start;
        }

        public Date getEnd() {
            return end;
        }

        public void setEnd(Date end) {
            this.end = end;
        }

        public boolean isContain(Date inDate){
            if(start==null||end==null)
                return false;
            if(TimeUtil.isAfter(inDate, start)&&TimeUtil.isBefore(inDate, end))
                return true;
            return false;
        }

        public boolean isBeforeStart(Date inDate){
            if(this.start==null)
                return true;
            return TimeUtil.isBefore(inDate, start);
        }

        public boolean isAfterEnd(Date inDate){
            if(this.end==null)
                return true;
            return TimeUtil.isAfter(inDate, end);
        }
       

        public Color getBackgroundColor() {
            return bg;
        }

        public void setBackgroundColor(Color bg) {
            this.bg = bg;
        }

        public Color getBorderColor() {
            return border;
        }

        public void setBorderColor(Color border) {
            this.border = border;
        }

        public Color getForegroundColor() {
            return fg;
        }

        public void setForegroundColor(Color fg) {
            this.fg = fg;
        }

        public boolean isSelected () {
		return selected;
	}

	public void setSelected ( boolean selected ) {
		this.selected = selected;
	}

        public Vector getEventInstancesForDate(int year, int month, int day) {
            Date date=TimeUtil.createDate(year, month-1, day);
            if(!isContain(date))
                 loadERPEvents(date);
            String YMD=FORMAT_YMD.format(date);
            return cachedERPEvents.get(YMD);
        }

        public void loadERPEvents(Date inDate){
        List<AbstractERPEvent> events=null; 
        if(start==null||end==null){
             Date before=TimeUtil.addDay(inDate, -ERPRepository.DAYCOUNT);
             events= repository.getEventInstancesForDate(before, inDate);
              setStart(before);
              setEnd(inDate);
        }else  if(isBeforeStart(inDate)){
              Date before=TimeUtil.addDay(this.getStart(), -ERPRepository.DAYCOUNT);
              events= repository.getEventInstancesForDate(before, getStart());
              setStart(before);
       }else if(isAfterEnd(inDate)||TimeUtil.isEqualTo(end, inDate)){
              Date end=TimeUtil.addDay(getEnd(), ERPRepository.DAYCOUNT);
              events= repository.getEventInstancesForDate(getEnd(), end);
              setEnd(end);
        }
        for ( int i = 0; events != null && i < events.size (); i++ ) {
            AbstractERPEvent event = events.get(i);
            event.setERPCalendar(this);
            Vector dateVector = null;
            String YMD = FORMAT_YMD.format(TimeUtil.GetDateNoTime(event.datetime));
            if (cachedERPEvents.containsKey(YMD)) {
                dateVector = (Vector) cachedERPEvents.get(YMD);
            } else {
                dateVector = new Vector();
                cachedERPEvents.put(YMD, dateVector);
            }
            dateVector.addElement(event);
        }
    }

    @Override
    public String toString() {
        return name;
    }


}
